<?php

class Kal_Banners_Block_Manage_Banners_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
	protected function _prepareForm()
	{
		$form = new Varien_Data_Form(array(
									  'id' => 'edit_form',
									  'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
									  'method' => 'post',
									'enctype' => 'multipart/form-data',
								   ));
		$this->setForm($form);
		$fieldset = $form->addFieldset('banners_form', array('legend'=>Mage::helper('banners')->__('Post information')));
		
		$fieldset->addField('title', 'text', array(
		  'label'     => Mage::helper('banners')->__('Title'),
		  'class'     => 'required-entry',
		  'required'  => true,
		  'name'      => 'title',
		));
		$fieldset->addField('nfile', 'image', array(
		  'label'     => Mage::helper('banners')->__('Banner Image'),
		  //'class'     => 'required-entry',
		  'required'  => false,
		  'name'      => 'nfile',
		));

		$fieldset->addField('link_url', 'text', array(
		  'label'     => Mage::helper('banners')->__('Link URL'),
		  //'class'     => 'required-entry',
		  'required'  => false,
		  'name'      => 'link_url',
		));
		/*
		$fieldset->addField('identifier', 'text', array(
		  'label'     => Mage::helper('banners')->__('Identifier'),
		  'class'     => 'required-entry',
		  'required'  => true,
		  'name'      => 'identifier',
		  'class'     => 'validate-identifier',
		  'after_element_html' => '<span class="hint">(eg: domain.com/banners/identifier)</span>',
		));*/
	  
	  	/**
         * Check is single store mode
         */
        if (!Mage::app()->isSingleStoreMode()) {
            $fieldset->addField('store_id', 'multiselect', array(
                'name'      => 'stores[]',
                'label'     => Mage::helper('cms')->__('Store View'),
                'title'     => Mage::helper('cms')->__('Store View'),
                'required'  => true,
                'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
            ));
        }
        

		
		$categories = array();
	  	$collection = Mage::getModel('banners/cat')->getCollection()->setOrder('sort_order', 'asc');
		foreach ($collection as $cat) {
			$categories[] = ( array(
				'label' => (string)$cat->getTitle(),
				'value' => $cat->getCatId()
				));
		}
		
	  	$fieldset->addField('cat_id', 'select', array(
                'name'      => 'cats[]',
                'label'     => Mage::helper('banners')->__('Category'),
                'title'     => Mage::helper('banners')->__('Category'),
                'required'  => true,
                'values'    => $categories,
     	));
		
		$fieldset->addField('status', 'select', array(
		'label'     => Mage::helper('banners')->__('Status'),
		'name'      => 'status',
		'values'    => array(
		  array(
			  'value'     => 1,
			  'label'     => Mage::helper('banners')->__('Enabled'),
		  ),
		
		  array(
			  'value'     => 2,
			  'label'     => Mage::helper('banners')->__('Disabled'),
		  ),
		  
		  array(
			  'value'     => 3,
			  'label'     => Mage::helper('banners')->__('Hidden'),
		  ),
		),

		//'after_element_html' => '<span class="hint">(Hidden Pages will not show in the banners but can still be accessed directly)</span>',
		));
	  			


		
		
		$fieldset->addField('post_content', 'editor', array(
            'name'      => 'post_content',
            'label'     => Mage::helper('banners')->__('Main Description'),
            'title'     => Mage::helper('banners')->__('Main Description'),
            'style'     => 'width:700px; height:200px;',
            'wysiwyg'   => true,
            
        )); 
        $fieldset = $form->addFieldset('banners_options', array('legend'=>Mage::helper('banners')->__('Layout Order Information')));
        
		$id= $this->getRequest()->getParam('id');
		$post = Mage::getModel('banners/post')->load($id);
		
		$categories = array();
	  	$collection = Mage::getModel('features/cat')->getCollection()->setOrder('sort_order', 'asc');
		foreach ($collection as $cat) {
			$categories[] = ( array(
				'label' => (string)$cat->getTitle(),
				'value' => $cat->getCatId()
				));
		}
		
	  	$fieldset->addField('types', 'multiselect', array(
                'name'      => 'types[]',
                'label'     => Mage::helper('banners')->__('Features'),
                'title'     => Mage::helper('banners')->__('Features'),
                'required'  => true,
                'values'    => $categories,
     	));
        
     	$array = split(',',$post->getTypes());
     	
     	if (!empty($array[0]))
     	{
     	    for ($i=0;$i<count($array);$i++)
	     	{
	     		$cat= Mage::getModel('features/cat')->load($array[$i]);
	     		
	  			$field = $cat->getIdentifier();
	     		$fieldname = $cat->getTitle();
	     		
	     		if ($field=='ice-skate') $field='iceskate';
	     		if ($field=='party-functions') $field='partyfunctions';
	     		
	     		$fieldset->addField('order_'.$field, 'text', array(
				'name' => 'order_'.$field,
				'label' => Mage::helper('banners')->__('Order In '.$fieldname .' Page'),
				'title' => Mage::helper('banners')->__('Order In '.$fieldname).' Page',
				'style' => 'width: 520px;',
				));
			
	     	}
     	}


        
		$form->setFieldNameSuffix('banners');
		
		if ( Mage::getSingleton('adminhtml/session')->getBannersData() )
		{
		  $form->setValues(Mage::getSingleton('adminhtml/session')->getBannersData());
		  Mage::getSingleton('adminhtml/session')->setBannersData(null);
		} elseif ( Mage::registry('banners_data') ) {
		  $form->setValues(Mage::registry('banners_data')->getData());
		}
		return parent::_prepareForm();
  }
}